<template>
  <div class="basin-dashboard">
    <div class="dashboard-header boxWrapper">
      <div class="header-content">
        <div class="header-left">
          <div class="basin-selector-container">
            <p class="title"><i class="el-icon-map-location"></i>当前所在流域</p>
            <el-select 
              v-model="basinId" 
              placeholder="选择流域" 
              class="basin-selector"
              @change="handleChangeBasin"
            >
              <el-option
                v-for="item in allBasin"
                :key="item.id"
                :label="item.basinName"
                :value="item.id"
              />
              <div slot="empty" class="empty-select">
                <i class="iconfont">&#xe64a;</i>
                <p>暂无选项</p>
              </div>
            </el-select>
          </div>
          
          <div class="date-picker-container">
            <p class="title"><i class="el-icon-date"></i>统计日期</p>
            <el-date-picker
              v-model="selectedDate"
              type="date"
              format="yyyy年MM月dd日"
              value-format="yyyy/MM/dd"
              placeholder="选择日期"
              class="date-picker"
              prefix-icon="el-icon-date"
              :clearable="false"
              @change="handleChangeDate"
            />
          </div>
        </div>
        
        <!-- 右侧数据统计区域 -->
        <div class="stats-overview">
          <div class="stat-card">
            <div class="stat-icon primary">
              <i class="iconfont">&#xe621;</i>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ stationCount || 0}}</div>
              <div class="stat-label">电站数量</div>
            </div>
          </div>
          
          <div class="stat-card">
            <div class="stat-icon success">
              <i class="iconfont">&#xe637;</i>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ tableDataInfo.installation || 0 }}</div>
              <div class="stat-label">装机容量(MW)</div>
            </div>
          </div>

          <div class="stat-card" v-for="(item, key) in tableDataInfo.damHeights" :key="key">
            <div class="stat-icon warning">
              <i class="iconfont">&#xe780;</i>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ item.count }}</div>
              <div class="stat-label">{{ item.heightIllustrate }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 主内容区域 -->
    <div class="dashboard-content">
      <!-- 左侧地图区域 -->
      <div class="left-column">
        <cardBox title="流域地图" icon="&#xe63b;">
          <template #right>
            <div class="map-legend">
              <div class="legend-item">
                <i class="iconfont greenText ico">&#xe60d;</i>
                <span>正常</span>
              </div>
              <div class="legend-item" >
                <i class="iconfont blueText ico">&#xe60d;</i>
                <span>III级</span>
              </div>
              <div class="legend-item" >
                <i class="iconfont orangeText ico">&#xe60d;</i>
                <span>II级</span>
              </div>
              <div class="legend-item" >
                <i class="iconfont redText ico">&#xe60d;</i>
                <span>I级</span>
              </div>
              
            </div>
          </template>
          <div class="marsWrapper">
            <div class="map-loading">
              <i class="iconfont">&#xe63b;</i>
              <div class="loading-text">地图加载中...</div>
            </div>
            <MarsMap ref="marsMap" :basinId="basinId" class="marsMap"/>
          </div>
        </cardBox>

        <div class="left-bottom">
          <cardBox title="监测数据实时情况" icon="&#xe6a0;">
            <div class="card-body" v-if="hasMonitorData" v-loading="loadding.monitorData">
              <div class="monitor-summary">
                <div class="monitor-overview">
                  <div class="overview-item">
                    <div class="overview-icon success">
                      <i class="el-icon-success"></i>
                    </div>
                    <div class="overview-content">
                      <div class="value">{{ ((monitorData['正常'] / monitorData['采集数据']) * 100 || 0).toFixed(1) }}%</div>
                      <div class="label">正常率</div>
                    </div>
                  </div>
                  <div class="overview-item">
                    <div class="overview-icon warning">
                      <i class="el-icon-warning"></i>
                    </div>
                    <div class="overview-content">
                      <div class="value">{{ ((monitorData['异常'] / monitorData['采集数据']) * 100 || 0).toFixed(1) }}%</div>
                      <div class="label">异常率</div>
                    </div>
                  </div>
                  <div class="overview-item">
                    <div class="overview-icon danger">
                      <i class="el-icon-error"></i>
                    </div>
                    <div class="overview-content">
                      <div class="value">{{ ((monitorData['错误'] / monitorData['采集数据']) * 100 || 0).toFixed(1) }}%</div>
                      <div class="label">错误率</div>
                    </div>
                  </div>
                </div>
                <!-- 总采集点 -->
                <div class="total-points">
                  <div class="label">总采集点</div>
                  <div class="value">{{ monitorData['采集数据'] }}</div>
                  <div class="sub-label">当前监测点总数</div>
                </div>
                
                <!-- 状态详情 -->
                <div class="status-details">
                  <div class="status-header">
                    <span>详细状态分布</span>
                    <el-tooltip content="各状态监测点数量统计" placement="top">
                      <i class="el-icon-info"></i>
                    </el-tooltip>
                  </div>
                  <div class="status-item">
                    <div class="status-info">
                      <span class="status-dot success"></span>
                      <span class="label">正常</span>
                      <el-progress 
                       :percentage="Number(((monitorData['正常'] || 0) / monitorData['采集数据'] * 100).toFixed(1))"
                        :stroke-width="8"
                        color="#67c23a"
                        :show-text="false"
                        class="status-progress"
                      />
                    </div>
                    <span class="value success">{{ monitorData['正常'] || 0 }}</span>
                  </div>

                  <div class="status-item">
                    <div class="status-info">
                      <span class="status-dot warning"></span>
                      <span class="label">异常</span>
                      <el-progress 
                       :percentage="Number(((monitorData['异常'] || 0) / monitorData['采集数据'] * 100).toFixed(1))"
                        :stroke-width="8"
                        color="#e6a23c"
                        :show-text="false"
                        class="status-progress"
                      />
                    </div>
                    <span class="value warning">{{ monitorData['异常'] || 0 }}</span>
                  </div>

                  <div class="status-item">
                    <div class="status-info">
                      <span class="status-dot danger"></span>
                      <span class="label">错误</span>
                      <el-progress 
                       :percentage="Number(((monitorData['错误'] || 0) / monitorData['采集数据'] * 100).toFixed(1))"
                        :stroke-width="8"
                        color="#f56c6c"
                        :show-text="false"
                        class="status-progress"
                      />
                    </div>
                    <span class="value danger">{{ monitorData['错误'] || 0 }}</span>
                  </div>

                  <div class="status-item">
                    <div class="status-info">
                      <span class="status-dot info"></span>
                      <span class="label">未判定</span>
                      <el-progress 
                       :percentage="Number(((monitorData['未判定'] || 0) / monitorData['采集数据'] * 100).toFixed(1))"
                        :stroke-width="8"
                        color="#409EFF"
                        :show-text="false"
                        class="status-progress"
                      />
                    </div>
                    <span class="value info">{{ monitorData['未判定'] || 0 }}</span>
                  </div>

                  <div class="status-item">
                    <div class="status-info">
                      <span class="status-dot secondary"></span>
                      <span class="label">漏测</span>
                      <el-progress 
                       :percentage="Number(((monitorData['漏测'] || 0) / monitorData['采集数据'] * 100).toFixed(1))"
                        :stroke-width="8"
                        color="#909399"
                        :show-text="false"
                        class="status-progress"
                      />
                    </div>
                    <span class="value secondary">{{ monitorData['漏测'] || 0 }}</span>
                  </div>
                </div>
              </div>
            </div>
            <noData showData style="height:320px" v-else/>
          </cardBox>

          <cardBox title="系统 / 台帐消息" icon="&#xe63e;">
            <template #right>
              <el-badge :value="Number(sysInfo.length + mainLogs.length)" class="badge" type="danger"></el-badge>
            </template>
            <div class="system-messages card-body" v-if="hasSysInfo" v-loading="loadding.outPoints">
              <div class="message-list">
                <div class="message-item" v-for="(item, index) in sysInfo" :key="index">
                  <div class="message-icon">
                    <i class="el-icon-message-solid"></i>
                  </div>
                  <div class="message-content">
                    <span
                      v-for="(part, i) in splitMessage(item)"
                      :key="i"
                      :class="{ number: isNumber(part)}"
                    >
                      {{ part }}
                    </span>
                    <p class="time">{{ selectedDate }}</p>
                  </div>
                </div>

                <div class="message-item" v-for="(item, index) in mainLogs" :key="index">
                  <div class="message-icon">
                    <i class="el-icon-message-solid"></i>
                  </div>
                  <div class="message-content">
                    <div>{{ item.Message }}</div>
                    <p class="time">{{ selectedDate }}</p>
                  </div>
                </div>
              </div>
            </div>
            <noData showData style="height:200px" v-else/>
          </cardBox>
        </div>
        
      </div>
      
      <!-- 中间状态信息区域 -->
      <div class="center-column">
        <cardBox title="环境量监测" icon="&#xe62e;">
          <template #right>
            <el-select 
              v-model="stationId" 
              placeholder="选择电站" 
              size="small"
              style="width: 140px;"
              @change="handleChangeStation"
            >
              <el-option
                v-for="item in stationList"
                :key="item.id"
                :label="item.stationName"
                :value="item.id"
              />
            </el-select>
          </template>
          <div class="card-body" v-if="hasEnvironment" v-loading="loadding.environmentalInfo">
            <environmenFactors :environment="environment"></environmenFactors>
          </div>
          <noData showData style="height:200px" v-else/>
        </cardBox>
        <cardBox title="监测设备状态" icon="&#xe6f7;">
          <div class="equipment-status" v-if="!isNoJC" v-loading="loadding.workStatus">
            <div class="card-body">
              <div class="equipment-chart">
                <div id="myChart" ref="chart" class="echart"></div>
              </div>
              <div class="equipment-legend">
                <div class="legend-item">
                  <span class="color-dot total"></span>
                  <span class="text">设备总数</span>
                  <span class="value">{{ chartData['总数'] || 0 }}</span>
                </div>
                <div class="legend-item">
                  <span class="color-dot rate"></span>
                  <span class="text">完好率</span>
                  <span class="value">{{ chartData['完好率'] || 0 }}%</span>
                </div>
                <div class="legend-item">
                  <span class="color-dot normal"></span>
                  <span class="text">正常在测</span>
                  <span class="value">{{ chartData['正常在测'] || 0 }}</span>
                </div>
                <div class="legend-item">
                  <span class="color-dot stop"></span>
                  <span class="text">停测</span>
                  <span class="value">{{ chartData['停测'] || 0 }}</span>
                </div>
                <div class="legend-item">
                  <span class="color-dot failure"></span>
                  <span class="text">封存</span>
                  <span class="value">{{ chartData['封存'] || 0 }}</span>
                </div>
                <div class="legend-item">
                  <span class="color-dot damage"></span>
                  <span class="text">损坏</span>
                  <span class="value">{{ chartData['损坏'] || 0 }}</span>
                </div>
                <div class="legend-item">
                  <span class="color-dot scrap"></span>
                  <span class="text">报废</span>
                  <span class="value">{{ chartData['报废'] || 0 }}</span>
                </div>
              </div>
            </div>
          </div>
          <noData showChart style="height:200px" v-else/>
        </cardBox>  
      </div>
      <!-- 右侧信息区域 -->
      <div class="right-column">
        <cardBox title="大坝健康性态" icon="&#xe694;">
          <div class="card-body" v-if="hasHealthData" v-loading="loadding.damHealth">
            <div class="health-summary">
              <div class="summary-item">
                <div class="number total">{{ healthData.stationInfos?.length || 0 }}</div>
                <div class="text">电站总数</div>
              </div>
              <div class="summary-item">
                <div class="number normal">{{ healthData.normal || 0 }}</div>
                <div class="text">健康电站</div>
              </div>
              <div class="summary-item">
                <div class="number abnormal">{{ abnormalStationCount || 0 }}</div>
                <div class="text">异常电站</div>
              </div>
            </div>
          
            <div class="health-list">
              <div class="station-item" v-for="(item, index) in healthData.stationInfos" :key="index">
                <span class="station-name">{{ item.stationName }}</span>
                <el-tag type="danger" size="small" v-if="item.judgement == 'I级'">
                  {{ item.judgement }}
                </el-tag>
                <el-tag type="success" size="small" v-if="item.judgement == '正常'">
                  {{ item.judgement }}
                </el-tag>
                <el-tag type="warning" size="small" v-if="item.judgement == 'II级'">
                  {{ item.judgement }}
                </el-tag>
                <el-tag type="primary" size="small" v-if="item.judgement == 'III级'">
                  {{ item.judgement }}
                </el-tag>
              </div>
            </div>
          </div>
          <noData showData style="height:200px" v-else/>
        </cardBox>
        <cardBox title="巡视评价" icon="&#xe666;">
          <div class="card-body" v-loading="loadding.patrolEvaluation">
            <div class="evaluation-stats">
              <div  class="stat">
                <div class="value total">{{ evaluateData['总数'] || 0 }}</div>
                <div class="label">总数</div>
              </div>
               <div  class="stat">
                <div class="value normal">{{ evaluateData['正常数量'] || 0 }}</div>
                <div class="label">正常</div>
              </div>
              <div  class="stat">
                <div class="value level3">{{ evaluateData['Ⅲ级别'] || 0 }}</div>
                <div class="label">Ⅲ级</div>
              </div>
               <div  class="stat">
                <div class="value level2">{{ evaluateData['Ⅱ级别'] || 0 }}</div>
                <div class="label">Ⅱ级</div>
              </div>
             
              <div  class="stat">
                <div class="value level1">{{ evaluateData['Ⅰ级别'] || 0 }}</div>
                <div class="label">Ⅰ级</div>
              </div>
            </div>
          </div>
        </cardBox>
        <cardBox title="大坝基本信息" icon="&#xe686;">
          <div class="card-body" v-loading="loadding.baseInfo">
            <div class="dams-grid" v-if="hasStationInfos">
              <el-carousel trigger="click" height="620px">
                <el-carousel-item v-for="(dam, index) in tableDataInfo.stationInfos" :key="index" >
                  <div class="dam-card">
                <!-- 顶部标题区域 -->
                <div class="dam-header">
                  <h4 class="dam-name">{{ dam.stationName }}</h4>
                  <el-tag 
                    :type="getSafeLevelType(dam.safeLevel)" 
                    size="small" 
                    effect="dark"
                    class="safety-tag"
                  >
                    {{ dam.safeLevel || '无等级' }}
                  </el-tag>
                </div>
                
                <!-- 基本信息区域 -->
                <div class="dam-basic-info">
                  <div class="info-row">
                    <div class="info-item">
                      <i class="iconfont">&#xea73;</i>
                      <span class="label">坝型:</span>
                      <span class="value">{{ dam.damCategory || '--' }}</span>
                    </div>
                    <div class="info-item">
                      <i class="iconfont">&#xe637;</i>
                      <span class="label">装机容量:</span>
                      <span class="value">{{ dam.installation || '--' }}</span>
                    </div>
                  </div>
                  <div class="info-row">
                    <div class="info-item">
                      <i class="iconfont">&#xe75a;</i>
                      <span class="label">坝长:</span>
                      <span class="value">{{ dam.damLen || '--' }}</span>
                    </div>
                    <div class="info-item">
                      <i class="iconfont">&#xe75a;</i>
                      <span class="label">坝高:</span>
                      <span class="value">{{ dam.damHeight || '--' }}</span>
                    </div>
                  </div>
                  
                  <div class="info-row">
                    <div class="info-item">
                      <i class="el-icon-time"></i>
                      <span class="label">投运时间:</span>
                      <span class="value">{{ dam._productionTime || '--' }}</span>
                    </div>
                  </div>
                </div>
                
                <!-- 时间信息区域 -->
                <div class="dam-time-info">
                  <div class="status-header">
                    <span>时间详情</span>
                  </div>
                  <div class="time-grid">
                    <div class="time-item">
                      <div class="time-icon">
                        <i class="el-icon-date"></i>
                      </div>
                      <div class="time-content">
                        <div class="time-label">下次换证时间</div>
                        <div class="time-value">{{dam._CorrectionTime || '--'}}</div>
                      </div>
                    </div>

                    <div class="time-item">
                      <div class="time-icon">
                        <i class="el-icon-date"></i>
                      </div>
                      <div class="time-content">
                        <div class="time-label">定检时间</div>
                        <div class="time-value">{{dam._inspectionTime || '--'}}</div>
                      </div>
                    </div>

                    <div class="time-item">
                      <div class="time-icon">
                        <i class="el-icon-date"></i>
                      </div>
                      <div class="time-content">
                        <div class="time-label">下次定检时间</div>
                        <div class="time-value">{{dam._inspectioTime1 || '--'}}</div>
                      </div>
                    </div>

                    <div class="time-item">
                      <div class="time-icon">
                        <i class="el-icon-date"></i>
                      </div>
                      <div class="time-content">
                        <div class="time-label">注册时间</div>
                        <div class="time-value">{{dam._registTime || '--'}}</div>
                      </div>
                    </div>
                  </div>
                </div>
                <!-- 状态信息区域 -->
                <div class="dam-status">
                  <div class="status-header">
                    <span>运行状态</span>
                  </div>
                  <div class="status-grid">
                    <div class="status-item status-normal">
                      <div class="status-icon">
                        <i class="iconfont">&#xe74f;</i>
                      </div>
                      <div class="status-content">
                        <div class="status-label">实时评价</div>
                        <div class="status-value">{{ dam.indexNameWorkStatus || '--' }}</div>
                      </div>
                    </div>

                    <div class="status-item status-unknown">
                      <div class="status-icon">
                        <i class="iconfont">&#xe644;</i>
                      </div>
                      <div class="status-content">
                        <div class="status-label">采集情况</div>
                        <div class="status-value">{{ dam.gather || '--' }}</div>
                      </div>
                    </div>

                    <div class="status-item status-warning">
                      <div class="status-icon">
                        <i class="iconfont">&#xe653;</i>
                      </div>
                      <div class="status-content">
                        <div class="status-label">异常情况</div>
                        <div class="status-value">{{ dam.abnormal || '--' }}</div>
                      </div>
                    </div>

                    <div class="status-item status-unknown">
                      <div class="status-icon">
                        <i class="iconfont">&#xe7b0;</i>
                      </div>
                      <div class="status-content">
                        <div class="status-label">巡视情况</div>
                        <div class="status-value">{{ dam.patrolStatus || '--' }}</div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              </el-carousel-item>
            </el-carousel>
            </div>
            <noData showData style="height:320px" v-else/>
          </div>
        </cardBox>
        
      </div>
    </div>
  </div>
</template>

<script>
import {GetWorkStatus,getOutPoints,GetStatisticLedger,GetBaseInfo,GetAllBasin,GetDamHealth,GetMonitorData,GetPatrolEvaluation,GetVnvironmentalInfo,GetStationByBasinID} from "@/api/https";
import MarsMap from '@/components/page/MarsMap.vue'
import bus from '@/utils/eventBus.js';
import noData from '@/components/base/noData.vue'
import cardBox from "../../components/base/cardBox.vue";
import environmenFactors from '@/components/page/index/environmentFactors.vue'
import DateUtils from '@/utils/dateUtils';
export default ({
    name:'basin',
    components:{MarsMap,noData,cardBox,environmenFactors},
    data() {
        return{
            selectedDate: '',
            basinId: '',
            chartData: {},
            sysInfo: [],
            mainLogs: [],
            errorCount: 0,
            tableDataInfo: {},
            healthData:{},
            monitorData: {},
            environment: [],
            evaluateData:{},
            allBasin: [],
            isNoJC: false,
            myChart:null,
            stationList:[],
            stationId:'',
            loadding: {
              baseInfo: true,
              damHealth: true,
              workStatus: true,
              patrolEvaluation: true,
              statisticLedger: true,
              environmentalInfo: true,
              monitorData: true,
              outPoints: true
            },
        }  
    },
    created(){
      this.selectedDate = DateUtils.formatDate(DateUtils.getDate('today'))
      bus.$emit('showBasin', false);
      this.onGetAllBasin()
    },
    activated(){
      bus.$emit('showBasin', false);
      this.drawChart()
    },
    deactivated(){
      this.destroyChart()
      bus.$off('showBasin');
    },
    beforeDestroy(){
      this.destroyMap()
      this.destroyChart()
      bus.$off('showBasin');
    },
    computed: {
      hasStationInfos() {
        return this.tableDataInfo.stationInfos?.length > 0;
      },
      stationCount() {
        return this.tableDataInfo.stationInfos?.length || 0;
      },
      hasHealthData() {
        return this.healthData.stationInfos?.length > 0;
      },
      abnormalStationCount() {
        return Number(this.healthData.abnormal1 + this.healthData.abnormal2 + this.healthData.abnormal3) || 0;
      },
      hasMainLogs() {
        return this.mainLogs.length > 0;
      },
      hasEnvironment() {
        return this.environment.length > 0;
      },
      hasMonitorData() {
        return this.monitorData['采集数据'];
      },
      hasSysInfo() {
        return this.sysInfo.length > 0;
      }
    },
    methods:{
      async init() {
        await Promise.all([
          this.fetchWorkStatus(),
          this.fetchOutPoints(),
          this.fetchStatisticLedger(),
          this.fetchMonitorData()
        ]);
      },

      async initDate() {
        await Promise.all([
          this.fetchBaseInfo(),
          this.fetchDamHealth(),
          this.fetchPatrolEvaluation()
        ]);
      },
      async fetchData(apiCall, loaddingKey, successCallback) {
        this.loadding[loaddingKey] = true;
        try {
          const res = await apiCall();
          if (res.isSuccess) {
            successCallback(res.data);
          } else {
            successCallback({});
          }
        } catch (error) {
          successCallback({});
        } finally {
          this.loadding[loaddingKey] = false;
        }
      },

      async fetchWorkStatus() {
        await this.fetchData(
          () => GetWorkStatus({ BasinID: this.basinId }),
          'workStatus',
          (data) => {
            if (data && data.type) {
              this.isNoJC = false;
              this.$nextTick(() => {
                this.chartDataAlive2 = data.type;
                this.drawChart();
                this.chartData = data.count || {};
              });
            } else {
              this.chartData = {};
              this.isNoJC = true;
              this.destroyChart();
            }
          }
        );
      },

      async fetchOutPoints() {
        await this.fetchData(
          () => getOutPoints({ BasinID: this.basinId }),
          'outPoints',
          (data) => {
            this.sysInfo = data?.info || [];
          }
        );
      },

      async fetchStatisticLedger() {
        await this.fetchData(
          () => GetStatisticLedger({ BasinID: this.basinId }),
          'statisticLedger',
          (data) => {
            this.mainLogs = data?.Ledgers || [];
            this.errorCount = data?.TotalCount || 0;
          }
        );
      },

      async fetchMonitorData() {
        await this.fetchData(
          () => GetMonitorData({ BasinID: this.basinId }),
          'monitorData',
          (data) => {
            this.monitorData = data || {};
          }
        );
      },

      async fetchBaseInfo() {
        await this.fetchData(
          () => GetBaseInfo({ BasinID: this.basinId, date: this.selectedDate }),
          'baseInfo',
          (data) => {
            this.tableDataInfo = data || {};
          }
        );
      },

      async fetchDamHealth() {
        await this.fetchData(
          () => GetDamHealth({ BasinID: this.basinId, time: this.selectedDate }),
          'damHealth',
          (data) => {
            this.healthData = data || {};
          }
        );
      },

      async fetchPatrolEvaluation() {
        await this.fetchData(
          () => GetPatrolEvaluation({ BasinID: this.basinId, date: this.selectedDate }),
          'patrolEvaluation',
          (data) => {
            this.evaluateData = data || {};
          }
        );
      },

      async fetchEnvironmentalInfo() {
        await this.fetchData(
          () => GetVnvironmentalInfo({ staionID: this.stationId, time: this.selectedDate }),
          'environmentalInfo',
          (data) => {
            this.environment = data || [];
          }
        );
      },
      async getStation() {
        try {
          const res = await GetStationByBasinID(this.basinId);
          if (res.isSuccess) {
            this.stationList = res.data || [];
            this.stationId = this.stationList[0]?.id || '';
            if (this.stationId) {
              await this.fetchEnvironmentalInfo();
            }
          }
        } catch (error) {
          this.stationList = [];
          this.loadding.environmentalInfo = false;
        }
      },
      splitMessage(message) {
        return message.split(/(\d+\.?\d*)/).filter((part) => part !== "");
      },

      isNumber(part) {
          return /^\d+\.?\d*$/.test(part);
      },
      async onGetAllBasin() {
        try {
          const res = await GetAllBasin({ ORGID: 0 });
          if (res.isSuccess) {
            this.allBasin = res.data;
            if (this.allBasin.length > 1) {
              this.basinId = this.allBasin[1].id;
              await this.getStation();
              await this.init();
              await this.initDate();
              this.$nextTick(() => {
                if (this.$refs.marsMap) {
                  this.$refs.marsMap.initMap();
                }
              });
            }
          }
        } catch (error) {
          console.error('Error getting all basin:', error);
        }
      },
      drawChart() {
        if (!this.chartDataAlive2 || !document.getElementById('myChart')) return;
        this.destroyChart();
        this.myChart = this.$echarts.init(document.getElementById('myChart'), null, { locale: "ZH" });

        const chartData = Object.entries(this.chartDataAlive2).map(([name, value]) => ({
          value,
          name: `${name}:(${value})`
        }));

        const option = {
          legend: {
            orient: 'vertical',
            right: 10,
            top: 30,
            bottom: 10,
          },
          series: [{
            minAngle: 5,
            type: 'pie',
            radius: ['40%', '80%'],
            center: ['30%', '50%'],
            avoidLabelOverlap: false,
            padAngle: 5,
            itemStyle: { borderRadius: 10 },
            label: { show: false, position: 'center'},
            emphasis: {
              label: {
                show: true,
                fontSize: 14,
                fontWeight: 'bold'
              }
            },
            labelLine: { show: false },
            data: chartData
          }]
        };

        this.myChart.setOption(option, true);
      },
      destroyMap() {
        if (this.$refs.marsMap) {
          this.$refs.marsMap.destroyMap?.();
        }
        if (window.Cesium) {
        // 清理 Cesium 静态缓存
        Cesium.Resource.clearCache();
        Cesium.CameraEventAggregator.prototype.destroy();
        // 确保所有实体移除
        this.map?.viewer?.entities?.removeAll();
        }
      },
      destroyChart() {
        if (this.myChart) {
          this.myChart.dispose();
          this.myChart = null;
        }
      },
      async handleChangeBasin(id) {
        this.destroyChart();
        this.basinId = id;
        await this.getStation();
        await this.init();
        await this.initDate();
        this.$nextTick(() => {
          if (this.$refs.marsMap) {
            this.$refs.marsMap.updateMapData?.();
          }
        });
      },
      handleChangeDate(){
        this.initDate()
      },
      async handleChangeStation(val) {
        this.stationId = val;
        await this.fetchEnvironmentalInfo();
      },
      getSafeLevelType(level) {
        if (!level) return 'info';
        const levelMap = {
          '甲级': 'success',
          'Ⅲ等': 'primary', '三类': 'primary',
          'Ⅱ等': 'warning', '二类': 'warning',
          'Ⅰ等': 'danger', '一类': 'danger'
        };
        return levelMap[level] || 'info';
      },
      

    }
})
</script>

<style lang="scss" scoped>
.basin-dashboard{
  padding-bottom: 20px;
}
// 顶部样式部分
.dashboard-header {
  padding: 12px 16px;
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .header-left {
    display: flex;
    align-items: center;
    gap: 16px;
    .title{
      line-height:30px;
      font-weight: bold;
      i{
        font-size: 16px;
        margin:0 6px;
      }
    }
    
    ::v-deep .basin-selector {
      width: 220px;
      
      .el-input__inner {
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        color: #334155;
        height: 36px;
        line-height: 36px;
        font-size: 14px;
        font-weight: 500;
        
        &::placeholder {
          color: #94a3b8;
        }
      }
      
      .el-input__suffix {
        .el-select__caret {
          color: #94a3b8;
        }
      }
    }
    
    ::v-deep .date-picker {
      width: 180px;
      
      .el-input__inner {
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        color: #334155;
        height: 36px;
        line-height: 36px;
        
        &::placeholder {
          color: #94a3b8;
        }
      }
      
      .el-input__prefix {
        i {
          color: #94a3b8;
        }
      }
      
      .el-input__suffix {
        .el-input__icon {
          color: #94a3b8;
        }
      }
    }
  }
  .stats-overview {
    display: flex;
    gap: 12px;
    .stat-card {
      display: flex;
      align-items: center;
      background: #f8fafc;
      border-radius: 8px;
      padding: 10px 14px;
      min-width: 140px;
      border: 1px solid #f1f5f9;
      transition: all 0.2s ease;
      
      &:hover {
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.04);
        transform: translateY(-1px);
      }
      
      .stat-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        border-radius: 8px;
        margin-right: 12px;
        font-size: 18px;
        
        &.primary {
          background: #eff6ff;
          color: #3b82f6;
        }
        
        &.success {
          background: #f0fdf4;
          color: #22c55e;
        }
        
        &.warning {
          background: #fffbeb;
          color: #f59e0b;
        }
      }
      
      .stat-content {
        .stat-value {
          font-size: 18px;
          font-weight: 700;
          line-height: 1.2;
          margin-bottom: 2px;
          color: #1e293b;
        }
        
        .stat-label {
          font-size: 12px;
          color: #64748b;
          font-weight: 500;
        }
      }
    }
  }
}

// 内容区域
.dashboard-content {
  display: grid;
  grid-template-columns: 1fr 430px 430px;
  gap: 10px;
  .boxWrapper{
    margin-top:10px;
  }
}
// 流域地图样式
.map-legend {
  display: flex;
  gap: 15px;
  margin-top:6px;
  .legend-item {
    display: flex;
    align-items: center;
    gap: 5px;
    .ico{
      font-size: 22px;
    }
  }
}   
.card-body{
  padding: 20px;
}
.left-bottom{
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px
}
// 监测数据实时情况
.monitor-summary {
  height: 530px;
  .monitor-overview {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    margin-bottom: 20px;
    
    .overview-item {
      display: flex;
      align-items: center;
      padding: 12px;
      background: #f9fafc;
      border-radius: 8px;
      
      .overview-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        border-radius: 8px;
        margin-right: 12px;
        font-size: 18px;
        
        &.success {
          background: rgba(103, 194, 58, 0.1);
          color: #67c23a;
        }
        &.warning {
          background: rgba(230, 162, 60, 0.1);
          color: #e6a23c;
        }
        &.danger {
          background: rgba(245, 108, 108, 0.1);
          color: #f56c6c;
        }
      }
      
      .overview-content {
        .value {
          font-size: 16px;
          font-weight: 700;
          color: #303133;
          margin-bottom: 2px;
        }
        .label {
          font-size: 12px;
          color: #909399;
        }
      }
    }
  }

  .total-points {
    text-align: center;
    margin-bottom: 20px;
    padding: 20px;
    background: linear-gradient(135deg, #ecf5ff, #d9ecff);
    border-radius: 8px;
    border: 1px solid #c0dfff;
    
    .label {
      font-size: 14px;
      color: #333;
      margin-bottom: 8px;
      font-weight: 500;
    }
    
    .value {
      font-size: 32px;
      font-weight: 800;
      color: #409EFF;
      margin-bottom: 4px;
    }
    
    .sub-label {
      font-size: 12px;
      color: #909399;
    }
  }
  
  .status-details {
    margin-bottom: 20px;
    
    .status-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 16px;
      padding-bottom: 8px;
      border-bottom: 1px solid #ebeef5;
      
      span {
        font-weight: 600;
        color: #303133;
        font-size: 14px;
      }
      
      i {
        color: #909399;
        cursor: pointer;
      }
    }
    
    .status-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
      font-size: 13x;
      padding: 12px 15px;
      background: #f9fafc;
      border-radius: 8px;
      
      &:last-child {
        border-bottom: none;
      }
      
      .status-info {
        display: flex;
        align-items: center;
        flex: 1;
        gap: 12px;
        
        
        .status-dot {
          width: 8px;
          height: 8px;
          border-radius: 50%;
          
          &.success { background: #67c23a; }
          &.warning { background: #e6a23c; }
          &.danger { background: #f56c6c; }
          &.info { background: #409EFF; }
          &.secondary { background: #909399; }
        }
        
        .label {
          font-size: 13px;
          color: #606266;
          min-width: 60px;
        }
        
        .status-progress {
          flex: 1;
          max-width: 120px;
        }
      }
      
      .value {
        font-size: 14px;
        font-weight: 600;
        min-width: 40px;
        text-align: right;
        
        &.success { color: #67c23a; }
        &.warning { color: #e6a23c; }
        &.danger { color: #f56c6c; }
        &.info { color: #409EFF; }
        &.secondary { color: #909399; }
      }
    }
  }
}

// 消息
.system-messages, .ledger-info {
  height: 550px;
  overflow-y: auto;
  .message-list, .ledger-list {
    .message-item, .ledger-item {
      display: flex;
      padding: 12px 0;
      border-bottom: 1px solid #f5f7fa;
      
      &:last-child {
        border-bottom: none;
      }
      
      .message-icon, .ledger-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        background: #ecf5ff;
        border-radius: 50%;
        margin-right: 12px;
        flex-shrink: 0;
        
        i {
          color: #409EFF;
          font-size: 16px;
        }
      }
      
      .message-content, .ledger-content {
        flex: 1;
        
        div {
          font-size: 13px;
          color: #606266;
          line-height:35px;
          margin-top:-8px;
        }
        
        .time {
          font-size: 12px;
          color: #909399;
          margin-top:6px;
        }
      }
    }
  }
}

//环境量
  .environment-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    
    .env-item {
      display: flex;
      align-items: center;
      padding: 15px;
      background: #f9fafc;
      border-radius: 8px;
      
      .env-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        border-radius: 8px;
        margin-right: 15px;
        font-size: 20px;
        
        &.temperature {
          background: #fff2e8;
          color: #fa541c;
        }
        
        &.rainfall {
          background: #e3f1ea;
          color: #1890ff;
        }
        
        &.water {
          background: #f0f9ff;
          color: #1890ff;
           i{
            font-size: 20px;
          }
        }
        
        &.flow {
          background: #fafff1;
          color: #7ace4f;
          i{
            font-size: 20px;
          }
        }
      }
      
      .env-info {
        .value {
          font-size: 18px;
          font-weight: 700;
          color: #303133;
          width:106px;
          text-overflow: ellipsis;
          overflow: hidden;
          height: 24px;
        }
        
        .label {
          font-size: 13px;
          color: #909399;
        }
      }
    }
  }

  // 监测设备状态
    .equipment-status {
    .equipment-chart {
      height: 180px;
      margin-bottom: 20px;
    }
    
    .equipment-legend {
      margin-top:50px;
      .legend-item {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        font-size: 14px;
        padding: 12px 15px;
        background: #f9fafc;
        border-radius: 8px;
        label {
          font-size: 14px;
          color: #606266;
        }
        .value {
            font-size: 16px;
            font-weight: 600;
        }
        
        .color-dot {
          width: 12px;
          height: 12px;
          border-radius: 50%;
          margin-right: 10px;
          
          &.total {
            background: #409EFF;
          }
          
          &.rate {
            background: #67c23a;
          }
          
          &.normal {
            background: #67c23a;
          }
          
          &.stop {
            background: #e6a23c;
          }
          
          &.failure {
            background: #f56c6c;
          }
          
          &.damage {
            background: #909399;
          }
          
          &.scrap {
            background: #303133;
          }
        }
        
        .text {
          flex: 1;
          font-size: 13px;
          color: #606266;
        }
        
        .value {
          font-size: 14px;
          font-weight: 600;
          color: #409EFF;
        }
      }
    }
  }
// 大坝健康性态
  .health-summary {
    display: flex;
    justify-content: space-around;
    margin-bottom: 20px;
    padding: 15px;
    background: #f9fafc;
    border-radius: 8px;
    
    .summary-item {
      text-align: center;
      
      .number {
        font-size: 28px;
        font-weight: 700;
        margin-bottom: 5px;
        
        &.total {
          color: #409EFF;
        }
        
        &.normal {
          color: #67c23a;
        }
        
        &.abnormal {
          color: #f56c6c;
        }
      }
      
      .text {
        font-size: 13px;
        color: #909399;
      }
    }
  }
  .health-list {
    .station-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 12px 0;
      border-bottom: 1px solid #f5f7fa;
      
      &:last-child {
        border-bottom: none;
      }
      
      .station-name {
        font-size: 14px;
        font-weight: 500;
      }
    }
  }
// 巡视
  .evaluation-stats {
    display: flex;
    justify-content: space-around;
    
    .stat {
      text-align: center;
      
      .value {
        font-size: 24px;
        font-weight: 700;
        margin-bottom: 5px;
        &.total{
          color:#333;
        }
        
        &.normal {
          color: #67c23a;
        }
        
        &.level1 {
          color: #f56c6c;
        }
        
        &.level2 {
          color: #e6a23c;
        }
        
        &.level3 {
          color: #409EFF;
        }
      }
      
      .label {
        font-size: 13px;
        color: #909399;
      }
    }
  }

  // 大坝信息
  .dams-grid{
  .dam-card {
  flex: 0 0 380px;
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.3s ease;
  border: 1px solid #ebeef5;
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  }
}

.dam-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f2f5;
  
  .dam-name {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #303133;
    line-height: 1.4;
  }
  
  .safety-tag {
    font-weight: 600;
    border-radius: 4px;
  }
}

.dam-basic-info {
  margin-bottom: 16px;
  
  .info-row {
    display: flex;
    margin-bottom: 12px;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
  
  .info-item {
    flex: 1;
    display: flex;
    align-items: center;
    font-size: 13px;
    
    i {
      margin-right: 6px;
      color: #909399;
      font-size: 14px;
    }
    
    .label {
      color: #606266;
      margin-right: 4px;
      font-weight: 500;
    }
    
    .value {
      color: #303133;
      font-weight: 600;
    }
  }
}

.dam-time-info {
  margin-bottom: 16px;
  border: none;
  
  ::v-deep .el-collapse-item__header {
    font-weight: 600;
    border: none;
    padding: 0;
    height: auto;
    line-height: 1.5;
  }
  
  ::v-deep .el-collapse-item__wrap {
    border: none;
  }
  
  ::v-deep .el-collapse-item__content {
    padding: 12px 0 0;
  }
  .status-header {
      font-weight: 600;
      margin-bottom: 12px;
      color: #303133;
      font-size: 14px;
    }
  
  .time-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }
  
  .time-item {
    display: flex;
    align-items: flex-start;
    
    .time-icon {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 24px;
      height: 24px;
      background: #f5f7fa;
      border-radius: 6px;
      margin-right: 8px;
      flex-shrink: 0;
      
      i {
        color: #409EFF;
        font-size: 14px;
      }
    }
    
    
    .time-content {
      .time-label {
        font-size: 12px;
        color: #909399;
        margin-bottom: 2px;
      }
      
      .time-value {
        font-size: 13px;
        color: #606266;
        font-weight: 500;
      }
    }
  }
}

.dam-status {
  .status-header {
    font-weight: 600;
    margin-bottom: 12px;
    color: #303133;
    font-size: 14px;
  }
  
  
  .status-item {
    display: flex;
    align-items: center;
    padding: 10px;
    border-radius: 8px;
    background: #f8f9fa;
    margin-bottom:10px;
    
    &.status-normal {
      background: rgba(103, 194, 58, 0.08);
      
      .status-icon {
        background: rgba(103, 194, 58, 0.12);
        
        i {
          color: #67c23a;
        }
      }
    }
    
    &.status-warning {
      background: rgba(230, 162, 60, 0.08);
      
      .status-icon {
        background: rgba(230, 162, 60, 0.12);
        
        i {
          color: #e6a23c;
        }
      }
    }
    
    &.status-unknown {
      .status-icon {
        background: rgba(144, 147, 153, 0.12);
        
        i {
          color: #909399;
        }
      }
    }
  }
  
  .status-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    margin-right: 10px;
    flex-shrink: 0;
    
    i {
      font-size: 16px;
    }
  }
  
  .status-content {
    .status-label {
      font-size: 12px;
      color: #909399;
      margin-bottom: 2px;
    }
    
    .status-value {
      font-size: 13px;
      color: #303133;
    }
  }
}
}

.number {
  color: #f56c6c;
  font-weight: 600;
}
.echart{
    height: 220px;
    width:100%;
    margin-top:-10px;
}
.marsWrapper{
  padding:20px;
  position: relative;
}
.map-loading{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height:400px;
  position: absolute;
  i{
    font-size: 30px;
    color: #999;
    margin-bottom: 10px;
  }
  .loading-text {
    color: #999;
    font-size: 16px;
    text-align: center;
  }
}
</style>